package cn.signalpha.modules.com.controller.api;

import cn.signalpha.common.constant.AppConstant;
import cn.signalpha.common.dto.IdDTO;
import cn.signalpha.core.boot.ctrl.BaseController;
import cn.signalpha.core.mp.support.Condition;
import cn.signalpha.core.mp.support.Query;
import cn.signalpha.core.tool.api.R;
import cn.signalpha.modules.com.entity.UserSign;
import cn.signalpha.modules.com.service.UserSignService;
import cn.signalpha.modules.com.vo.UserSignToDayVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 控制器
 *
 * @author signalpha
 */
@RestController
@AllArgsConstructor
@RequestMapping(AppConstant.APPLICATION_USER_NAME + "/wap/user/sign")
@Api(value = "签到", tags = "签到")
public class ApiUserSignController extends BaseController {

	private UserSignService userSignService;

	@GetMapping("/list")
	@ApiOperation(value = "签到明细记录", notes = "")
	public R<IPage<UserSign>> list(Query query) {
		IPage<UserSign> pages = userSignService.page(Condition.getPage(query), Wrappers.<UserSign>query().lambda()
			.eq(UserSign::getUserId, getUser().getUserId()).orderByDesc(UserSign::getId)
		);
		return R.data(pages);
	}

    @GetMapping("/info")
    @ApiOperation(value = "签到日历", notes = "传入年月")
    public R<UserSignToDayVO> info(@ApiParam(value = "年", required = true) @RequestParam Integer year,
								   @ApiParam(value = "月", required = true) @RequestParam Integer month) {
        return R.data(userSignService.selectSignIn(getUser().getUserId(), year, month));
    }

	@PostMapping("/signIn")
	@ApiOperation(value = "签到", notes = "")
	public R signIn() {
		boolean result = userSignService.saveSignIn(getUser().getUserId());
		return R.success(result ? "签到成功" : "签到失败");
	}

	@PostMapping("/signIn/retroactive")
	@ApiOperation(value = "补签到", notes = "id里面直接传入当月第几天，如7号直接传7")
	public R signInRetroactive(@Valid @RequestBody IdDTO idDTO) {
		boolean result = userSignService.saveSignInRetroactive(getUser().getUserId(), Integer.valueOf(idDTO.getId()));
		return R.success(result ? "补签到成功" : "补签到失败");
	}

}
